using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using MyLiEda.Events.Bus;
using MyLiEda.Events.Common;
using MyLiEda.Events.DapperEventStore;
using MyLiEda.Events.HandlerExecuteContext;
using MyLiEda.Events.RabbitMQ;
using RabbitMQ.Client;

namespace MyLiEda.Api
{
    public class Startup
    {
        private ILogger logger;
        private const string RMQ_EXCHANGE = "MyLiEdaRMQ.Exchange";
        private const string RMQ_QUEUE = "MyLiEdaRMQ.Queue";
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;           
            
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            //registe eventhandler 
            services.AddTransient<IEventHandler, MyLiEventHandler>();

            //add dapper EventStore
            services.AddTransient<IEventStore>(serviceProvider => new DapperEventStore(Configuration["mssql:connectionString"],
                serviceProvider.GetRequiredService<ILogger<DapperEventStore>>()
                ) ) ;

            //
            var eventHandlerExecuteContext = new EventHandlerExecuteContext(services,
                                serviceProviderFactory: sc =>sc.BuildServiceProvider());
            services.AddSingleton<IEventHandlerExecuteContext>(eventHandlerExecuteContext);

            //regist eventBus object
            //services.AddSingleton<IEventBus,PassThroughEventBus>();

            var connectionFactory = new ConnectionFactory { HostName = "localhost" };
            services.AddSingleton<IEventBus>(sp => new RabbitMQEventBus(connectionFactory,
                sp.GetRequiredService<ILogger<RabbitMQEventBus>>(),
                sp.GetRequiredService<IEventHandlerExecuteContext>(),
                RMQ_EXCHANGE,
                queueName: RMQ_QUEUE));
        }        

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
            eventBus.Subscribe<MyLiEvent, MyLiEventHandler>();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
